﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Chowpoo.Sql;
using CourseSystem.Core.Entities;
using CourseSystem.Core.SqlServerDAL;

namespace CourseSystem.Core.BLL
{
    public class CommonResourcesBLL : CommonBLL<CommonResourcesInfo, CommonResourcesDAL>
    {
        public static IQueryable<CommonResourcesInfo> GetList(byte type, byte minType, string search)
        {
            var re = GetList();
            if (!string.IsNullOrEmpty(search))
                re = re.Where(o => o.Title.Contains(search));
            re = re.Where(o => o.Type == type);
            if (minType == 0)
                return re;
            return re.Where(o => o.MinType == minType);
        }

        public static CommonResourcesInfo Get(byte type, byte minType)
        {
            using (var dal = new CommonResourcesDAL())
            {
                if (minType == 0)
                    return dal.SelectSingle(o => o.Type == type);
                var re = dal.SelectSingle(o => o.Type == type && o.MinType == minType);
                return re;
            }
        }

        public static CommonResourcesInfo GetByIdType(int id, byte type)
        {
            using (var dal = new CommonResourcesDAL())
            {
                var re = dal.SelectSingle(o => o.Type == type && o.Id == id);
                return re;
            }
        }

        public static CommonResourcesInfo Get(int id)
        {
            using (var dal = new CommonResourcesDAL())
            {
                var re = dal.SelectSingle(o => o.Id == id);
                return re;
            }
        }

        public static bool Del(int id)
        {
            using (var dal = new CommonResourcesDAL())
            {
                return dal.Delete(o => o.Id == id) != null;
            }
        }
    }
}
